$(document).ready(function(){
    
    active_menu();
    setTimeout(function(){
        var heightSocial = jQuery('.share_social').height();
        jQuery('.support').css('top', heightSocial + 10);
    },1000);
    
    $('a.scroll-top').click(function(){
            $('html, body').animate({scrollTop : 0},800);
    		return false;
        });
    
    jQuery(function(){
      test()
        jQuery(window).resize(function(){test()})
        function test(){
            var windowWidth = jQuery(window).width();
            var windowHeight = jQuery(window).height();
            //console.log(windowHeight);
            var width_nav_menu = jQuery('.nav-menu').width();
            jQuery('#menu-mobile').css('width', windowWidth - (windowWidth/3));
			jQuery('#menu-mobile').css('max-height', windowHeight-30);
			jQuery('#menu-mobile').css('overflow', 'auto');
            if(windowWidth < windowHeight || is_mobile != '1')
            {
                //var heightIntro = jQuery('.ctn-intro').height();
                var heightIntro = 460;
                var mgTop = (windowHeight - heightIntro) / 2;
                jQuery('.ctn-intro').css('margin-top', mgTop);
                jQuery('.adv-intro').css('margin-top', mgTop);
            }
            else
            {
                jQuery('.ctn-intro').css('margin-top', '0');
            }
            
            if(windowWidth < 768)
            {
                $('#menu-mobile').addClass('menu_active');
                $('#menu-desktop').removeClass('menu_active');
            }
            else
            {
                $('#menu-desktop').addClass('menu_active');
                $('#menu-mobile').removeClass('menu_active');
            }
        }
    });
    
    $('.tabdetail li a').click(function(){
        var tab = $(this).attr('href');
        $('.tabdetail li').removeClass('active');
        $(this).parents('li').addClass('active');
        $('.tabcontent .tab').hide();
        $(tab).fadeIn();
        if(tab == '#comment')
        {
            FB.XFBML.parse();
        }
        return false;
    });
    
    $('#newsletter').find('input[type=text]').keyup(function(){
        var email = $('#newsletter').find('input[type=text]').val();
        if(!checkmail(email))
        {
            $(this).addClass('error');
            return false;
        }
        else
        {
            $(this).removeClass('error');
            return false;
        }
    });
    
    $('#newsletter').submit(function(){
        var email = $('#newsletter').find('input[type=text]').val();
        var sex = $('#newsletter').find('input[type=radio]:checked').val();
        if(!checkmail(email))
        {
            $('#newsletter').find('input[type=text]').addClass('error');
            return false;
        }
        else
        {
            $('#newsletter').find('input[type=text]').removeClass('error');
            $.ajax({
				url:base_url + 'ajax/newsletter',
				type:'post',
				data:{email:email, sex:sex},
				success:function(data){
                    if(data == 1)
                    {
                        $('#newsletter').find('input[type=text]').val('');
                        $('#newsletter').find('input[type=radio]').prop('checked', false);
                        $('#newsletter').find('label span').css('color', '#FFF');
                        alert('Đăng ký thành công.');
                    }
                    else
                    {
                        alert('Email này đã đăng ký.');
                    }
                    
                    
				}
			});	
        }
        return false;
    });
    
    $('a[order=order]').click(function(){
        var product_id = $(this).attr('product_id');
        $.ajax({
				url:base_url + 'ajax/order',
				type:'post',
				data:{product_id:product_id},
				success:function(data){
                    var link = base_url + 'gio-hang';
                    window.location.href = link;
				}
			});	
    });
    
    $('a[order=delete]').click(function(){
        var product_id = $(this).attr('product_id');
        $.ajax({
				url:base_url + 'ajax/delete_cart',
				type:'post',
				data:{product_id:product_id},
				success:function(data){
                    var link = base_url + 'gio-hang';
                    window.location.href = link;
				}
			});	
    });
    
    $('select[order=change]').change(function(){
        var product_id = $(this).attr('product_id');
        var number = $(this).val();
        $.ajax({
				url:base_url + 'ajax/change_cart',
				type:'post',
				data:{product_id:product_id, number:number},
				success:function(data){
                    var link = base_url + 'gio-hang';
                    window.location.href = link;
				}
			});	
    });
    
    $('a[favorite=favorite]').click(function(){
        var product_id = $(this).attr('product_id');
        var type = $(this).attr('type');
        var $this = $(this);
        $.ajax({
				url:base_url + 'ajax/favorite',
				type:'post',
				data:{product_id:product_id, type:type},
				success:function(data){
                    if(data == 1)
                    {
                        var _this = $this;
                        _this.append('<span><img width="12" src="'+ webroot +'images/check.png" /></span>');
                    }
				}
			});	
    });
    
    $('.rating-stars').click(function(){
        //var percent = $(this).arrt('')
        setTimeout(function(){
            var rating_detail = $('input.rating-detail').val();
            var product_id = $('input.rating-detail').attr('product_id');
            $.ajax({
				url:base_url + 'ajax/rating',
				type:'post',
				data:{product_id:product_id, rating_detail:rating_detail},
				success:function(data){
                    alert('Cảm ơn bạn đã đánh giá.');
				}
			});	
        },100);
        
    });
    
    $('.lang_code a').click(function(){
        var lang_code = $(this).attr('lang_code');
        $.ajax({
				url:base_url + 'ajax/change_lang_code',
				type:'post',
				data:{lang_code:lang_code},
				success:function(data){
				    var link = base_url;
                    window.location.href = link;
                    //location.reload();
                    return false;
				}
			});
        return false;
    });
    
    $('.main-image a').click(function(){return false;});
    $('.sub-images a').click(function(){return false;});
    
    
    
    $('#menu-mobile > ul > li').hover(function(){
        var color = $(this).attr('color');
        $(this).css('background', '#'+color);
    },function(){
        $(this).removeAttr('style');
    });
    /*
    $('.menu_all_catee').click(function(){
        if($("#menu-desktop" ).hasClass("menu_active"))
        {
            $('.navigation-container').toggle();
        }
        else
        {
            $('.menu-mobile').toggle();
        }
        
    });
    */
});
function checkmail(email){
	var emailfilter=/^\w+[\+\.\w-]*@([\w-]+\.)*\w+[\w-]*\.([a-z]{2,4}|\d+)$/i;
	var returnval=emailfilter.test(email)
	return returnval;
}

function active_menu()
{
    var menu_active = $('input[name=cate_code_active]').val();
    $('ul.main-menu-2').find('a').each(function(index, element){
        if($(element).attr('linkto') == menu_active){
            console.log(element);
            $(element).css('color', '#FFF');
        }
            
    })
}
var i = 0;
function openMenu()
{
    /*
    if($("#menu-desktop" ).hasClass("menu_active"))
    {
        //$('.navigation-container').toggle();
        if(i == 0){
            $('.navigation-container').show();
            i=1;
        }else{
            $('.navigation-container').hide();
            i=0;
        }
    }
    else
    {
	*/
        //$('.menu-mobile').toggle();
        if(i == 0){
            $('.menu-mobile').show();
            i=1;
        }else{
            $('.menu-mobile').hide();
            i=0;
        }
    //}
}